package ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.sxpjyy.monkey.mobilenurse.R;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import adapters.BuzRecyclerAdapter;
import mytools.MyBaseActivity;
import objects.InPatient;

public class PatientDetailActivity extends MyBaseActivity {

    private TextView patientName;
    private BottomNavigationView navigation;

    private RecyclerView recyclerView;
    private BuzRecyclerAdapter mRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_patient_detail);
        setNeedsMenuKey();

        onBindUI();
        mRecyclerAdapter = new BuzRecyclerAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(mRecyclerAdapter);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        mRecyclerAdapter.setOnItemClickListener((view, data) -> {
            Intent intent;
            switch (data) {
                case "计划复核":
                    intent = new Intent(PatientDetailActivity.this, OrdersCheckActivity_2.class);
                    startActivity(intent);
                    break;
//                    case "计划管理":
//                        intent = new Intent(PatientDetailActivity.this, OrdersManageActivity.class);
//                        startActivity(intent);
//                        break;

                case "计划执行":
                    intent = new Intent(PatientDetailActivity.this, OrdersExecuteActivity.class);
                    startActivity(intent);
                    break;
//                    case "计划执行":
//                        intent = new Intent(PatientDetailActivity.this, DocOrdersActivity.class);
//                        bundle.putParcelable("user", user);
//                        intent.putExtras(bundle);
//                        startActivity(intent);
//                        break;
                case "费用查询":
                    intent = new Intent(PatientDetailActivity.this, CostActivity.class);
                    startActivity(intent);
                    break;
                case "检验报告":
                    intent = new Intent(PatientDetailActivity.this, ReportJYActivity_2.class);
                    startActivity(intent);
                    break;
                case "检查报告":
                    intent = new Intent(PatientDetailActivity.this, ReportJCActivity.class);
                    startActivity(intent);
                    break;
                case "健康宣教":
                    intent = new Intent(PatientDetailActivity.this, HEActivity.class);
//                        intent = new Intent(PatientDetailActivity.this, HeathEducateActivity.class);
                    startActivity(intent);
                    break;
                default:
                    Toast.makeText(PatientDetailActivity.this, data + " clicked", Toast.LENGTH_SHORT).show();
                    break;
            }
        });
    }

    private void onBindUI() {
        navigation = findViewById(R.id.navigation);
        patientName = findViewById(R.id.patient_detail_name);
        recyclerView = findViewById(R.id.recyler_button);
    }

    @Override
    protected void onResume() {
        super.onResume();
        InPatient inPatient = getMyApplication().getInPatient();
        if (inPatient != null) {
            patientName.setText(inPatient.getBRXM());
            loadBtns();
        } else {
            Toast.makeText(this, "获取病人数据失败，请重新登陆！", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, LoginActivity.class));
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            int visibility = navigation.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE;
            navigation.setVisibility(visibility);
        }
        return super.onKeyDown(keyCode, event);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = item -> {
                Intent intent;
                switch (item.getItemId()) {
                    case R.id.navigation_change: //切换病区
                        intent = new Intent(PatientDetailActivity.this, SectionsListActivity.class);
                        startActivity(intent);
                        return true;
                    case R.id.navigation_list: //病人列表
                        intent = new Intent(PatientDetailActivity.this, PatientsListActivity.class);
                        startActivity(intent);
                        return true;
                    case R.id.navigation_setting: //设置
                        Toast.makeText(PatientDetailActivity.this, "not yet", Toast.LENGTH_SHORT).show();
                        return true;
                }
                return false;
            };

    //加载按钮
    private void loadBtns() {
        List<String> names = new ArrayList<>();
        List<Integer> images = new ArrayList<>();
        String[] btn_values = {"医嘱明细", "计划复核", "计划执行", "费用查询", "检验报告", "检查报告", "健康宣教", "按钮7", "按钮8", "按钮9"};
        for (int i = 0; i < btn_values.length; i++) {
            int imageID;
            String picname;
            switch (btn_values[i]) {
                case "医嘱明细":
                    picname = "ic_doctor";
                    break;
                case "计划复核":
                    picname = "ic_report";
                    break;
                case "计划执行":
                    picname = "ic_syringe";
                    break;
                case "费用查询":
                    picname = "ic_dollar";
                    break;
                case "检验报告":
                    picname = "ic_examination";
                    break;
                case "检查报告":
                    picname = "ic_xray";
                    break;
                case "健康宣教":
                    picname = "ic_info";
                    break;
//                case "计划复核":
//                    picname = "ic_report";
//                    break;
                default:
                    picname = String.format(Locale.CHINA,"ic_num%02d", i);
                    break;
            }
            imageID = getResources().getIdentifier(picname, "drawable", getApplicationContext().getPackageName());
            images.add(imageID);
            names.add(btn_values[i]);
        }
        if (names.size() > 0) {
            mRecyclerAdapter.setButtonText(names);
            mRecyclerAdapter.setButtonImage(images);
        }
    }

    //显示虚拟菜单按钮
    private void setNeedsMenuKey() {
        try {
            Method setNeedsMenuKey = Window.class.getDeclaredMethod("setNeedsMenuKey", int.class);
            setNeedsMenuKey.setAccessible(true);
            int value = WindowManager.LayoutParams.class.getField("NEEDS_MENU_SET_TRUE").getInt(null);
            setNeedsMenuKey.invoke(getWindow(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
